﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using AnalisisDeRiesgos.Dal;

namespace AnalisisDeRiesgos.Models.Report
{
    public class PotentialDangersInfo
    {
        public int DangerId { get; set; }
        public string Management { get; set; }
        public string Process { get; set; }
        public string Area { get; set; }
        public string Activity { get; set; }
        public string Danger { get; set; }
        public string Risk { get; set; }         
        public string Frequence { get; set; }
        public string ExposedNumber { get; set; }
        public string Controls { get; set; }
        public string Severity { get; set; }
        public string PosibleControls { get; set; }
        public string ExpirationDate { get; set; }
        public string Status { get; set; }
        public string Probabilites { get; set; }
        public string Level { get; set; }
        public int LevelValue { get; set; }
        public string Importance { get; set; }
        public string Term { get; set; }
        public string Actual { get; set; }
        public string NewValue { get; set; }
        public string Newlevel { get; set; }
        public string KFactor { get; set; }

        public void CalculateRisk(danger theDanger)
        {
            int frecuenceVal = theDanger.frecuence;
            int exposedVal = theDanger.exposed;
            int controlsVal = theDanger.controlExistence;
            int severityVal = theDanger.severity;
            int frequence;

            switch (frecuenceVal)
            {
                case 1:
                    frequence = 10;
                    break;
                case 2:
                    frequence = 30;
                    break;
                case 3:
                    frequence = 50;
                    break;
                case 4:
                    frequence = 70;
                    break;
                case 5:
                    frequence = 100;
                    break;
                default:
                    frequence = 0;
                    break;
            }

            int exposed;

            switch (exposedVal)
            {
                case 1:
                    exposed = 30;
                    break;
                case 2:
                    exposed = 60;
                    break;
                case 3:
                    exposed = 100;
                    break;
                default:
                    exposed = 0;
                    break;
            }

            int controls;
            switch (controlsVal)
            {
                case 1:
                    controls = 20;
                    break;
                case 2:
                    controls = 70;
                    break;
                case 3:
                    controls = 100;
                    break;
                default:
                    controls = 0;
                    break;
            }

            double probability = frequence * 0.4 + exposed * 0.3 + controls * 0.3;
            string probabilityText = "";
            int risklevel = 0;
            int[][] levelArray = new int[][] { new int[]{ 1, 3, 3, 4, 4},
                                        new int[]{ 1, 2, 3, 3, 4},
                                        new int[]{ 0, 1, 2, 3, 3},
                                        new int[]{ 0, 0, 1, 2, 3},
                                        new int[]{ 0, 0, 1, 1, 2}};
            string[] levelList = new string[] {"Riesgo Bajo",
                                "Riesgo Moderado",
                                "Riesgo Importante",
                                "Riesgo Alto",
                                "Riesgo Intolerable"};
            string[] termList = new string[]{ "Incluir en la mejora a largo Plazo",
                            "Acciones hasta 3 años",
                            "Acciones en 1 año",
                            "Acciones en 3 meses",
                            "Paro, Acciones Inmediatas"};
            double[] riskValues = new double[] { 2.4, 3.4, 4.9, 7, 10 };

            if (probability < 35)
            {
                probabilityText = "Muy baja";
                risklevel = levelArray[4][severityVal - 1];
            }
            else if (probability < 51.1)
            {
                probabilityText = "Baja";
                risklevel = levelArray[3][severityVal - 1];
            }
            else if (probability < 67.1)
            {
                probabilityText = "Media";
                risklevel = levelArray[2][severityVal - 1];
            }
            else if (probability < 83.1)
            {
                probabilityText = "Alta";
                risklevel = levelArray[1][severityVal - 1];
            }
            else
            {
                probabilityText = "Muy Alta";
                risklevel = levelArray[0][severityVal - 1];
            }

            string significance = risklevel >= 2 ? "SI" : "NO";
            double thenewVal = riskValues[risklevel] * Convert.ToDouble(theDanger.kFactor) / 10;
            int thenewlevel = 0;

            if (thenewVal <= 2.4)
                thenewlevel = 0;
            else if (thenewVal > 2.4 && thenewVal <= 3.4)
                thenewlevel = 1;
            else if (thenewVal > 3.4 && thenewVal <= 4.9)
                thenewlevel = 2;
            else if (thenewVal > 4.9 && thenewVal <= 7)
                thenewlevel = 3;
            else if (thenewVal > 7 && thenewVal <= 10)
                thenewlevel = 4;
            else
                thenewlevel = 0;
            Probabilites = probabilityText;
            Level = levelList[risklevel];
            LevelValue = risklevel;
            Importance = significance;
            Term = termList[risklevel];
            Actual = levelList[risklevel];
            NewValue = thenewVal.ToString();
            Newlevel = levelList[thenewlevel];
        }
    }
}